<template>
  <div class="start">
    <!-- <van-nav-bar title="标题" left-arrow @click-left="onClickLeft" /> -->
    <div style="height: 2.2rem;paddingTop:2rem; overflow: hidden">
      <dl style="padding-right: 1rem">
        <dd style="float: right; margin-left: 0.5rem">
          <img
            v-if="!headimgurl"
            style="border-radius: 50%; height: 2.2rem; width: 2.2rem"
            src="../assets/tu.jpg"
            alt=""
          />
          <img
            v-else
            style="border-radius: 50%; height: 2.2rem; width: 2.2rem"
            :src="headimgurl"
            alt=""
          />
        </dd>

        <dt
          v-if="!nickname"
          style="
            float: right;
            line-height: 1rem;
            margin-top: 0.7rem;
            font-size: 0.8rem;
            color: #fff;
          "
        >
          小兔
        </dt>
        <dt
          v-else
          style="
            float: right;
            line-height: 1rem;
            margin-top: 0.7rem;
            font-size: 0.8rem;
            color: #fff;
          "
        >
          {{ nickname }}
        </dt>
      </dl>
    </div>
    <div class="whitediv">
      <img src="../assets/finish.png" alt="" />
      <p>恭喜答题完成</p>
      <p>填写领奖信息</p>
      <van-form
        @submit="onSubmit"
        @failed="onFailed"
        :show-error="true"
        :show-error-message="false"
        label-align="right"
        :colon="true"
      >
        <van-cell-group inset>
          <!-- 通过 pattern 进行正则校验 -->
          <van-field
            label-width="3.5em"
            class="inp"
            v-model="value1"
            label="姓名"
            name="pattern"
            placeholder="请输入姓名"
            :rules="[{ pattern, message: '请填写密码' }]"
          />
          <!-- 通过 validator 进行函数校验 -->
          <van-field
            label-width="3.5em"
            v-model="value2"
            name="validator"
            label="手机号"
            placeholder="请输入手机号"
            :rules="[{ validator, message: '请输入正确内容' }]"
          />
          <!-- 通过 validator 返回错误提示 -->
          <!-- <van-field
            label-width="3.5em"
            v-model="value3"
            label="身份证"
            name="validatorMessage"
            placeholder="请输入身份证"
            :rules="[{ validator: validatorMessage }]"
          /> -->
        </van-cell-group>
        <div style="margin: 8px">
          <van-button block class="subbtn" native-type="submit">
            填写完成
          </van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>
<script>
import { ref } from "vue";
import { Toast } from "vant";
import { useRouter } from "vue-router";
import { useStore } from 'vuex'
import axios from "axios";
export default {
  created() {
    console.log(localStorage.getItem("token"))
    console.log(localStorage.getItem("uid"))
    if (localStorage.getItem("name") && localStorage.getItem("img")) {
      this.nickname = localStorage.getItem("name");
      this.headimgurl = localStorage.getItem("img");
    }
    this.$store.commit("setque",-1)
  },
  setup() {
    const nickname = ref();
    const headimgurl = ref();
    const router = useRouter();
    const store=useStore()
    const value1 = ref("");
    const value2 = ref("");
    const value3 = ref("");
    const value4 = ref("");
    const pattern = /^[\u4e00-\u9fa5]{2,4}$/;

    // 校验函数返回 true 表示校验通过，false 表示不通过
    const validator = (val) => /^[1][3,4,5,7,8,9][0-9]{9}$/.test(val);

    // 校验函数可以直接返回一段错误提示
    const validatorMessage = (val) =>
      /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(
        val
      );

    const onSubmit = async (values) => {
      var res = await axios.post("/api/login/usermessage", {
        name: values.pattern,
        phone: values.validator,
        // card: values.validatorMessage,
        username:localStorage.getItem("name"),
        openid:localStorage.getItem("openid"),
        access_token: localStorage.getItem("token"),
        uid: localStorage.getItem("uid"),
      });
      if (res.data.code == 200) {
        Toast.success("信息填写成功!");
        router.push("/Message");
        localStorage.setItem("alreadyfinish",true)
        store.commit("setque",0)
        store.commit("setone",false)
      } else if (res.data.code == 201) {
        Toast.fail("未登录,请先登录!");
      } else if (res.data.code == 202) {
        Toast.fail("参与失败!");
      } else if (res.data.code == 203) {
        Toast.fail("每人只能参与一次活动!");
      } else if (res.data.code == 204) {
        Toast.fail("活动人数已满!");
        router.push("/Thanks");
      }

      // console.log("submit", values);
      // console.log(res);
    };
    const onFailed = (errorInfo) => {
      if (errorInfo) {
        Toast.fail("格式不正确,请重新填写!");
      }
    };
    const onClickLeft = () => history.back();
    return {
      value1,
      value2,
      value3,
      value4,
      pattern,
      validator,
      onFailed,
      validatorMessage,
      onClickLeft,
      onSubmit,
      nickname,
      headimgurl,
    };
  },
};
</script>
<style scoped>
.start {
  height: 100%;
  background: url("../assets/dise.png");
  position: relative;
}
.whitediv {
  height: 77%;
  position: fixed;
  background-color: white;
  top: 23%;
  /* bottom: 0; */
  left: 0;
  width: 100%;
  border-radius: 15px 15px 0 0;
}
.whitediv img {
  width: 50%;
  height: 35%;
  margin-left: 25%;
}
.whitediv p {
  font-size: 0.75rem;
  text-align: center;
  font-weight: bold;
  margin-bottom: 0.3rem;
}
.van-field__control {
  height: 1.4rem;
  border: 1px solid #ccc !important;
}
.subbtn {
  background: #1f62e6;
  color: white;
  width: 50%;
  height: 1.5rem;
  border-radius: 8px;
  margin-left: 25%;
  /* margin-bottom: 1rem; */
}
</style>